<?php
namespace app\work\controller;
use app\work\logic\DataLogic;

/**
 * 每日数据api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/10/31
 * Time: 11:12
 */
class Data extends Base {

    public function __construct(){
        parent::__construct();
        $this->d_logic = new DataLogic();
    }

    /**
     * 获取每日数据
     * @return \think\response\Json
     */
    public function ajax_get_data(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查用户规则
        $r = $this->d_logic->check_user_rule();
        if($r['code'] != 1){
            return json($r);
        }
        $u_detail = $r['data']['detail'];

        $today_time = get_today_time();
        $today_start_date = date('Y-m-d',strtotime($today_time['start_time']));
        //$today_end_date = date('Y-m-d',strtotime($today_time['end_time']));

        //计算当前月“目前金额”
        $wrd = new \app\common\controller\WorkRecordData();
        $now_money = $wrd->get_month_now_money($u_detail['department'],$today_start_date);

        //获取“目标金额”
        $wrd = new \app\common\controller\WorkRecordData();
        $target_money = $wrd->get_month_target_money($u_detail['department'],$today_start_date);

        $data['total_now_money'] = $now_money;
        $data['target_money'] = $target_money;
        return json($this->d_logic->get_response(1,'OK',$data));
    }

    /**
     * 新增每日数据
     * @return \think\response\Json
     */
    public function ajax_add_data(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查用户规则
        $r = $this->d_logic->check_user_rule();
        if($r['code'] != 1){
            return json($r);
        }
        $u_detail = $r['data']['detail'];

        //检查新增每日数据规则
        $r = $this->d_logic->check_add_data_rule($u_detail['department'],$params);
        if(!$r['code']){
            return json($r);
        }

        $today_time = get_today_time();
        $today_start_date = date('Y-m-d',strtotime($today_time['start_time']));
        //$today_end_date = date('Y-m-d',strtotime($today_time['end_time']));

        $maps = [];
        $maps[] = ['openid','=',$u_detail['openid']];
        $maps[] = ['now_date','=',$today_start_date];
        $wrd_detail = model('work_record_data')->get_detail($maps,'id');
        if(empty($wrd_detail)){
            $wcd_data = [];
            $wcd_data['openid'] = $u_detail['openid'];
            $wcd_data['department'] = $u_detail['department'];
            $wcd_data['today_shop_person_num'] = trim($params['today_shop_person_num']);
            $wcd_data['front_money'] = deal_number_mul_100(trim($params['front_money']));
            $wcd_data['vip_back_money'] = deal_number_mul_100(trim($params['vip_back_money']));
            $wcd_data['check_in_back_money'] = deal_number_mul_100(trim($params['check_in_back_money']));
            $wcd_data['agency_back_money'] = deal_number_mul_100(trim($params['agency_back_money']));
            $wcd_data['account_back_money'] = deal_number_mul_100(trim($params['account_back_money']));
            $wcd_data['trademark_back_money'] = deal_number_mul_100(trim($params['trademark_back_money']));
            $wcd_data['science_money'] = deal_number_mul_100(trim($params['science_money']));
            $wcd_data['other_money'] = deal_number_mul_100(trim($params['other_money']));
            $wcd_data['today_total_money'] = deal_number_mul_100(trim($params['today_total_money']));
            $wcd_data['pic'] = json_encode($params['pic'],JSON_UNESCAPED_UNICODE);
            $wcd_data['now_date'] = $today_start_date;
            $wrnid = model('work_record_data')->insertGetId($wcd_data);

            //更新发布每日数据积分
            $si = new \app\common\controller\SaveIntegral();
            $r = $si->save_r_data_integral($wrnid);
        }else{
            $wcd_data = [];
            $wcd_data['department'] = $u_detail['department'];
            $wcd_data['today_shop_person_num'] = trim($params['today_shop_person_num']);
            $wcd_data['front_money'] = deal_number_mul_100(trim($params['front_money']));
            $wcd_data['vip_back_money'] = deal_number_mul_100(trim($params['vip_back_money']));
            $wcd_data['check_in_back_money'] = deal_number_mul_100(trim($params['check_in_back_money']));
            $wcd_data['agency_back_money'] = deal_number_mul_100(trim($params['agency_back_money']));
            $wcd_data['account_back_money'] = deal_number_mul_100(trim($params['account_back_money']));
            $wcd_data['trademark_back_money'] = deal_number_mul_100(trim($params['trademark_back_money']));
            $wcd_data['science_money'] = deal_number_mul_100(trim($params['science_money']));
            $wcd_data['other_money'] = deal_number_mul_100(trim($params['other_money']));
            $wcd_data['today_total_money'] = deal_number_mul_100(trim($params['today_total_money']));
            $wcd_data['pic'] = json_encode($params['pic'],JSON_UNESCAPED_UNICODE);
            $wcd_data['now_date'] = $today_start_date;

            $maps = [];
            $maps[] = ['id','=',$wrd_detail['id']];
            $r = model('work_record_data')->force()->save($wcd_data,$maps);
        }
        if($r !== false){
            //同步发送工作圈
            $r = $this->d_logic->sync_send_wcm($u_detail['openid'],$u_detail['department'],$params);

            return json($this->d_logic->get_response(1,'提交成功'));
        }
        return json($this->d_logic->get_response(1,'提交失败'));
    }
}
